package com.leyou.item.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.item.mapper.CategoryMapper;
import com.ly.plus.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.List;

/**
 * 分类表业务层
 */
@Service
@Transactional
public class CategoryService extends ServiceImpl<CategoryMapper, Category> {
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    public List<Category> findCategoryByPid(Long pid) {
        //封装查询对象
        Category category = new Category();
        category.setParentId(pid);
        LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery(category);
        List<Category> categoryList = super.list(wrapper);//调用父类方法
        if (categoryList == null||categoryList.size()==0) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }

        return categoryList;
    }

    /**
     * 根据id集查询分类
     * @param ids
     * @return
     */
    public List<Category> findCategorysByIds(List<Long> ids) {
        List<Category> categories = categoryMapper.selectBatchIds(ids);
        if (CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return categories;
    }
}
